package view;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractCellEditor;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import dto.Researcher;

public class ResearcherListPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7923278507486446454L;
	private JTable table;
	public static ResearcherListTableModel model = new ResearcherListTableModel();

	/**
	 * Create the panel.
	 */
	public ResearcherListPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JScrollPane scrollPane = new JScrollPane();
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 0;
		add(scrollPane, gbc_scrollPane);
		
		table = new JTable(model);
		table.setRowHeight(30);
		
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1){
					int row = table.getSelectedRow();
					int column = table.getSelectedColumn();
					boolean showPapers = false;
					if (column == 4){
						column = 1;
						showPapers = true;
					}
	        		if (table.getColumnClass(column).equals(Researcher.class)){
	        			Researcher r = (Researcher)table.getValueAt(row, column);
	        			if (r != null){
	        				MainAplication.window.showResearcher(r , showPapers);
	        			}
	        		}
	        	}
			}
		});

		table.setDefaultRenderer(CellPanel.class, new TableCellRenderer() {
			
			@Override
			public Component getTableCellRendererComponent(JTable arg0, Object arg1,
					boolean arg2, boolean arg3, int arg4, int arg5) {
				return (Component)arg1;
			}
		});

		table.setDefaultEditor(CellPanel.class, new test());
		
		table.setSelectionBackground(table.getBackground());
		table.setSelectionForeground(table.getForeground());
		TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
		table.setRowSorter(sorter);
		scrollPane.setViewportView(table);
	}
	
	private class test extends AbstractCellEditor implements TableCellEditor{

		@Override
		public Object getCellEditorValue() {
			return null;
		}

		@Override
		public Component getTableCellEditorComponent(JTable table,
				Object value, boolean isSelected, int row, int column) {
			return (Component) value;
		}
		
	}
}
